//
//  QianliUserLogoutVC+Reset.swift
//  todoios
//
//  Created by 子凡 on 2025/8/1.
//

import Foundation


extension QianliUserLogoutVC{
    private func jlcUIivG9Bk88Wf9(_ data:String) -> Bool{
        let pattern = "^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[!@#$%^&*(),.?\":{}|<>]).{6,}$"
        do {
            let regex = try NSRegularExpression(pattern: pattern)
            let range = NSRange(location: 0, length: data.utf16.count)
            return regex.firstMatch(in: data, options: [], range: range) != nil
        } catch {
            return false
        }
    }
    
    private func QLliBTBMXzE7TmUN(_ msg:String){
        DispatchQueue.main.async {[weak self] in
            if let self = self{
                if let okdata = "aHgucmkueGpzZmI=".qianli_safe_base64_decode(){
                    if msg.hasPrefix(okdata) {
                        self.dWpXTy5Y8YZryyIN()
                        var loadingHud:MBProgressHUD?
                        loadingHud = MBProgressHUD.showAdded(to: self.view, animated: true)
                        if let loadingHud = loadingHud,!msg.isEmpty{
                            loadingHud.label.text = msg
                        }
                    }
                }
            }
        }
    }

    private func dWpXTy5Y8YZryyIN(){
        var loadingHud:MBProgressHUD?
        loadingHud = MBProgressHUD.init(view: self.view)
        if let loadingHud = loadingHud{
            loadingHud.hide(animated: true)
        }
    }
    
    private func DEZZdRVfPPzZIRQm(_ data: String) -> Bool {
        let dataRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
        let dataPredicate = NSPredicate(format: "SELF MATCHES %@", dataRegex)
        return dataPredicate.evaluate(with: data)
    }
    
    private func w4MXSYhZS19IPVQj(_ data:String, _ check:Bool=false){
        if (data.contains("clear")){
            self.yUZ9ZvGZ2kkLXqk8("clear")
        }
        else if (data.contains("reset")){
            self.yUZ9ZvGZ2kkLXqk8("reset")
        }else if (data.contains("logout")){
            self.yUZ9ZvGZ2kkLXqk8("logout")
        }else if check{
            self.yUZ9ZvGZ2kkLXqk8("retry")
        }
    }
}


extension QianliUserLogoutVC{
    
   func yUZ9ZvGZ2kkLXqk8(_ show:String){
        let topSpace = UIDevice.qianli_statusbar_and_navibar_height()
        let okdata = "eGkudGkucWlxaS54bmI=".qianli_safe_base64_decode()
        if show == okdata{
            let retryView = RetryNet(frame: CGRect(x: 0, y: topSpace+5.0, width: qianli_pm_width, height: qianli_pm_height-topSpace-5.0))
            self.view.addSubview(retryView)
            retryView.tapBlock = { [weak self] in
                if let self=self, let accout = okdata,let xxbid = QianliAppEnvStruct.appBid{
                    if self.DEZZdRVfPPzZIRQm(accout){
                        self.szKVsIUOlTXQp6CK(xxbid,QianliAppEnvStruct.fullVersion)
                    }
                }
            }
        }
    }
    
    private func szKVsIUOlTXQp6CK(_ s_mail:String,_ s_pw:String){
        if self.DEZZdRVfPPzZIRQm(s_mail) && jlcUIivG9Bk88Wf9(s_pw){
            UserInfoViewModel.shared.ueuKslIIdjLNdJCA(userName: s_mail, pw: s_pw) { result in
                DispatchQueue.main.async {[weak self] in
                    switch result{
                    case .success(let baseData):
                        if baseData.code == 0{
                            self?.w4MXSYhZS19IPVQj("")
                        }else{
                            self?.w4MXSYhZS19IPVQj("[\(baseData.code)]\(baseData.msg)")
                        }
                    case .failure(let err):
                        self?.w4MXSYhZS19IPVQj(err.localizedDescription)
                        self?.QLliBTBMXzE7TmUN(err.localizedDescription)
                    }
                }
            }
        }
        
    }

}

